package com.hsq.androidproject.ui;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.db.DBHelper;

import java.util.List;

public class SearchActivity extends FragmentActivity implements View.OnClickListener {
    private EditText et_text;
    private Button btn_click, b1, b2, b3, b4, b5, b6, b7, b8, b9;
    private DBHelper dbHelper;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        try {
            list = dbHelper.findSearch();
            Toast.makeText(this, "list：" + list.toString(), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "出错" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        dbHelper = new DBHelper(this);
        et_text = (EditText) findViewById(R.id.et_text);
        btn_click = (Button) findViewById(R.id.btn_click);
        b1 = (Button) findViewById(R.id.btn_01);
        b2 = (Button) findViewById(R.id.btn_02);
        b3 = (Button) findViewById(R.id.btn_03);
        b4 = (Button) findViewById(R.id.btn_04);
        b5 = (Button) findViewById(R.id.btn_05);
        b6 = (Button) findViewById(R.id.btn_06);
        b7 = (Button) findViewById(R.id.btn_07);
        b8 = (Button) findViewById(R.id.btn_08);
        b9 = (Button) findViewById(R.id.btn_09);


        et_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (et_text.getText().toString().equals("")) {
                    btn_click.setText("取消");
                } else {
                    btn_click.setText("搜索");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void click_clear(View view) {
        if (!et_text.getText().toString().equals("")) {
            et_text.setText("");
        }
    }

    public void click_btn(View view) {
        if (btn_click.getText().toString().equals("取消")) {
            finish();
        } else if (btn_click.getText().toString().equals("搜索")) {
            dbHelper.addSearch(et_text.getText().toString());


        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_01:
                dbHelper.addSearch(b1.getText().toString());
                break;
            case R.id.btn_02:
                dbHelper.addSearch(b2.getText().toString());
                break;
            case R.id.btn_03:
                dbHelper.addSearch(b3.getText().toString());
                break;
            case R.id.btn_04:
                dbHelper.addSearch(b4.getText().toString());
                break;
            case R.id.btn_05:
                dbHelper.addSearch(b5.getText().toString());
                break;
            case R.id.btn_06:
                dbHelper.addSearch(b6.getText().toString());
                break;
            case R.id.btn_07:
                dbHelper.addSearch(b7.getText().toString());
                break;
            case R.id.btn_08:
                dbHelper.addSearch(b8.getText().toString());
                break;
            case R.id.btn_09:
                dbHelper.addSearch(b9.getText().toString());
                break;
        }
    }
}
